﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unis.Framework.Core.Utility;

using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.CompletionMaterialsDAL.BcdProjectService;
using KingRoad.EPM.CompletionMaterialsDAL.BcdProjectFileService;
using KingRoad.EPM.CompletionMaterialsDAL.BcdFileDataService;


namespace KingRoad.EPM.CompletionMaterialsBLL.UIRules
{
    /// <summary> 
    /// 工程项目表 
    /// 文件生成时间 2017/5/23 9:56:09
    /// </summary> 
    public class BcdProjectUIRule
    {
        private readonly IBcdProjectService _bcdProjectService;
        private BcdProjectfileUIRule _bcdProjectfileUIRule;
        private BcdFiledataUIRule _bcdFiledataUIRule;

        public BcdProjectUIRule() { }

        /// <summary> 
        /// construct function... 
        /// </summary> 
        public BcdProjectUIRule(IBcdProjectService bcdProjectService)
        {
            _bcdProjectService = bcdProjectService;
        }

        /// <summary>
        /// 根据条件获取信息
        /// </summary>
        /// <returns></returns>
        public List<BcdProjectInfo> GetEntityListBySql(string sql)
        {
            var ret = _bcdProjectService.GetEntityList(sql);
            return ret;
        }
        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public BcdProjectInfo GetEntityById(Guid id)
        {
            var ret = _bcdProjectService.GetEntityById(id);
            return ret;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertEntity(BcdProjectInfo entity)
        {
            var ret = _bcdProjectService.InsertEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(BcdProjectInfo entity)
        {
            var ret = _bcdProjectService.UpdateEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="proId">项目id</param>
        /// <param name="fid">类型id</param>
        /// <returns></returns>
        public bool DeleteEntity(string proId, string fid)
        {
            int ret = 0;
            _bcdProjectfileUIRule = new BcdProjectfileUIRule(new BcdProjectfileService());
            List<BcdProjectfileInfo> pfList = _bcdProjectfileUIRule.GetEntityListBySql(" P_ID='"+proId+"' and ( Fd_PIds like '%"+fid+"%' or Fd_ID='"+fid+"') ");
            foreach (BcdProjectfileInfo pfInfo in pfList)
            {
                //同时删除 资料类型关系表中 有关该项目的 数据
                _bcdProjectfileUIRule.DeleteEntity(pfInfo);
            }
            _bcdFiledataUIRule = new BcdFiledataUIRule(new BcdFiledataService());
            BcdFiledataInfo fInfo = _bcdFiledataUIRule.GetEntityById(new Guid(fid));
            if (fInfo.Fd_PID == null) //如果是根节点 则 删除项目
                ret = _bcdProjectService.DeleteEntityByStringID(proId);   
            return ret > 0;
        }
    }
}

